package com.varton.plugin;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.util.Properties;

@Intercepts({@Signature(type = Executor.class,method = "query",args = {MappedStatement.class,Object.class, RowBounds.class, ResultHandler.class})})
public class QueryPlugin implements Interceptor{
    public Object intercept(Invocation invocation) throws Throwable {
        String className = invocation.getTarget().getClass().getName();
        String methName = invocation.getMethod().getName();
        Object[] os = invocation.getArgs();
        MappedStatement s = null;
        Object param = null;
        for (Object o : os){
            if (o instanceof MappedStatement){
                s = (MappedStatement) o;
            }
            if (o  != null && !(o instanceof RowBounds || o instanceof ResultHandler || o instanceof MappedStatement)){
                param = o;
            }
        }
        System.out.println("sql:"+s.getBoundSql(param).getSql());
        System.out.println("param:"+param);
        return invocation.proceed();
    }

    public Object plugin(Object target) {
        return Plugin.wrap(target,this);
    }

    public void setProperties(Properties properties) {

    }
}
